package com.paso.serverinfo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.paso.mycommons.vo.RespBean;
import com.paso.mycommons.vo.RespBeanEnum;
import com.paso.serverinfo.pojo.Serverinformation;
import com.paso.serverinfo.service.IServerinformationService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangx
 * @since 2021-02-12
 */
@RestController
@Slf4j
@RequestMapping("/serverinformation")
public class ServerinformationController {
    @Resource
    IServerinformationService iServerinformationService;

//    @JwtToken
    @RequestMapping("/hello")
    public String hell() {
        return "hello";
    }



//
//    @RequestMapping("/list")
//    public RespBean list() {
//        if (null != iServerinformationService.list()){
//            return  RespBean.success(iServerinformationService.list());
//        }
//        return RespBean.error(RespBeanEnum.ERROR);
//    }

    /**
     * 添加一条记录
     * @param serverinformation
     * @return
     */
//    @JwtToken
    @RequestMapping("/add")
    public RespBean save(Serverinformation serverinformation) {
        boolean save = iServerinformationService.saveOrUpdate(serverinformation);
        if (!save) {
            return RespBean.error(RespBeanEnum.ERROR);
        }

        return RespBean.success();
    }

    /**
     * 更新一条记录
     * @param serverinformation
     * @return
     */
//    @JwtToken
    @RequestMapping("/edit")
    public RespBean saveOrUpdate(Serverinformation serverinformation) {
        boolean save = iServerinformationService.saveOrUpdate(serverinformation);
        if (!save) {
            return RespBean.error(RespBeanEnum.ERROR);
        }

        return RespBean.success();
    }

    /**
     * 通过ID查询记录
     * @param id
     * @return
     */
//    @JwtToken
    @RequestMapping("/getById")
    public RespBean getById(long id) {
        if (null != iServerinformationService.getById(id)){
            return  RespBean.success(iServerinformationService.getById(id));
        }
        return RespBean.error(RespBeanEnum.ERROR);
    }

//    @JwtToken
    @RequestMapping("/removeById")
    public RespBean removeById(long id) {
        boolean b = iServerinformationService.removeById(id);
        if (!b) {
            return RespBean.error(RespBeanEnum.ERROR);
        }
        return RespBean.success();
    }

    /**
     * 根据IP地址1查询记录
     * @param serverIp1
     * @return
     */
//    @JwtToken
    @RequestMapping("/getListByIp")
    public RespBean getListByIp(String serverIp1) {
        if (null == serverIp1 || StringUtils.isEmpty(serverIp1)) {
            return  RespBean.success(iServerinformationService.list());
        }
        Serverinformation serverinformation = new Serverinformation();
        log.info(serverIp1);
        serverinformation.setServerIp1(serverIp1);
        if (null != iServerinformationService.list(new QueryWrapper(serverinformation))){
            return  RespBean.success(iServerinformationService.list(new QueryWrapper(serverinformation)));
        }
        return RespBean.error(RespBeanEnum.ERROR);
    }


//    @JwtToken
    @RequestMapping("/getListByIp1")
    public RespBean getListByIp1(@RequestParam("serverIp1") String serverIp1, @RequestParam("current") int current, @RequestParam("size") int size) {

        // 如果条件为空
        if (null == serverIp1 || StringUtils.isEmpty(serverIp1)) {
            return  RespBean.success(iServerinformationService.page(new Page<>(current, size)));
        }

        // 封装条件为对象
        Serverinformation serverinformation = new Serverinformation();
        serverinformation.setServerIp1(serverIp1);

        // 添加条件查询
        if (null != iServerinformationService.page(new Page<>(current, size), new QueryWrapper(serverinformation))){
            return  RespBean.success(iServerinformationService.page(new Page<>(current, size), new QueryWrapper(serverinformation)));
        }
        return RespBean.error(RespBeanEnum.ERROR);
    }

    /**
     * 查询所有记录，分页
     * @param current
     * @param size
     * @return
     */

//    @JwtToken
    @RequestMapping(value = "/pageList", method = RequestMethod.GET)
    public RespBean pageList(@RequestParam("current") int current, @RequestParam("size") int size) {

        Page<Serverinformation> page = iServerinformationService.page(new Page<>(current, size));
        System.out.println("=====================");
        return  RespBean.success(page);
    }


}